package com.spboot;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 自动生成器类
 * 
 */
public class GeneratorServiceEntity {

	public static void main(String[] args) {
		new GeneratorServiceEntity().generateCode();
	}
   
    public void generateCode() {
        String packageName = "com.spboot";
        //user -> UserService, 设置成true: user -> IUserService
        boolean serviceNameStartWithI = false;
        
        generateByTables(serviceNameStartWithI, packageName,"product");
    }

    /**
     * 
     * @param serviceNameStartWithI user -> UserService, 设置成true: user -> IUserService
     * @param packageName 包名
     * @param tableNames 表名(选填)，不写就是全部表，可以写一个或多个"表一","表二"...
     */
    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
      
      //数据源配置
	  String dbUrl = "jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8";
      DataSourceConfig dataSourceConfig = new DataSourceConfig();
      dataSourceConfig.setDbType(DbType.MYSQL)
              .setUrl(dbUrl)
              .setUsername("root")
              .setPassword("root")
			  .setDriverName("com.mysql.cj.jdbc.Driver");
      
        //全局配置
		GlobalConfig config = new GlobalConfig();
	    config.setActiveRecord(false)
	            .setAuthor("guocb")
	            .setOutputDir("E:\\T8072\\spboot\\src\\main\\java")
	            .setFileOverride(true)//文件是否覆盖
	            .setBaseResultMap(true)//sql隐射文件中是否生成 baseResultMap
	    		.setBaseColumnList(false);//sql隐射文件中是否生成 BaseColumnList

	    //生成策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true).setEntityLombokModel(true)//实体类Lombok风格
                .setRestControllerStyle(false)//控制器是否rest风格 RestController
                .setEntityColumnConstant(true)//实体类中生成列的常量
                .setControllerMappingHyphenStyle(false)//控制器 请求mapping下划线风格  true -> role_perm false -> rolePerm 
                .setTablePrefix("smbms_")//去除表名前缀
                .setNaming(NamingStrategy.underline_to_camel)//下划线转驼峰underline_to_camel: user_name -> userName,no_change : userName -> userName
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        		
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }

        //包名配置
        PackageConfig pkConfig =   new PackageConfig();
        pkConfig.setParent(packageName)
                .setController("controller")
                .setEntity("entity")
        		.setMapper("mapper")//dao
        		.setService("service")//service
        		.setXml("mapper");//xml
        
        //执行
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(pkConfig)
                .execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
